-- Server Side

if (not file.IsDir("TDM/FirstK","DATA")) then
    file.CreateDir("TDM/FirstK")
end
if not(file.Exists("TDM/FirstK/FirstK.txt","DATA")) then
	file.Write("TDM/FirstK/FirstK.txt", 0)
	--print("[TDM] Wrote file : TDM/FirstK/FirstK.txt")
	MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-FirstKill",Color(255,255,255),"] Wrote file: TDM/FirstK/FirstK.txt\n")
end
file.Write("TDM/FirstK/FirstK.txt", 0)

if (not file.IsDir("TDM/GoalP","DATA")) then
    file.CreateDir("TDM/GoalP")
end
if not(file.Exists("TDM/GoalP/GoalP.txt","DATA")) then
	file.Write("TDM/GoalP/GoalP.txt", -1)
	--print("[TDM] Wrote file : TDM/GoalP/GoalP.txt")
	MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-Goal",Color(255,255,255),"] Wrote file: TDM/GoalP/GoalP.txt\n")
end

function TDMEditGoalP(len, ply)
if !ply:IsSuperAdmin() then 
		umsg.Start("apple_tdm_notadmin", ply) -- Sends the message to player
		umsg.End()
return end
file.Write("TDM/GoalP/GoalP.txt", math.Round(net.ReadString( )))
	for k,v in pairs(player.GetAll()) do 
		umsg.Start("apple_tdm_setgoal", v) -- Sends the message to player
			umsg.Short(file.Read("TDM/GoalP/GoalP.txt"))
		umsg.End()
	end
end
net.Receive("apple_tdm_customgoal", TDMEditGoalP)

local SDB = "apple_tdm_challenges_c16"
if sql.TableExists(SDB) then
--MsgN("[TDM-ChallengeP] Database '"..SDB.."' Already Exists!")
MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-ChallengeDB",Color(255,255,255),"] Database '"..SDB.."' Already Exists!\n")
	else
		if (!sql.TableExists(SDB)) then
			local Query = "CREATE TABLE "..SDB.." ( ID int, PID varchar(255), PName varchar(255), C_Rank varchar(255), C_Crouch varchar(255) )"
			local Result = sql.Query(Query)
			if (sql.TableExists(SDB)) then
				--MsgN("[TDM-ChallengeP] Successfully Created Database '"..SDB.."'!")
				MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-ChallengeDB",Color(255,255,255),"] Successfully Created Database '"..SDB.."'!\n")
			else
				--MsgN("[TDM-ChallengeP] Database '"..SDB.."' Was Not Created!")
				MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-ChallengeDB",Color(255,255,255),"] Database '"..SDB.."' Was Not Created!\n")
				MsgN( sql.LastError( Result ) )
			end	
			
		end
end



function PlayerInitialSpawnTDMC(ply)
ply:SetPData("TDMLastKill", 0)

local GetLastPlayerIdC = sql.Query( "SELECT * FROM "..SDB.." WHERE PID = "..tostring(ply:UniqueID()).."" )
if GetLastPlayerIdC then 
	--MsgN("[TDM-ChallengeP] Player Already Exists!")
	MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-ChallengeAdd",Color(255,255,255),"] Player Already Exists!\n")
return end

local LastChallengeIDCreated2 = tonumber(sql.QueryValue( "SELECT ID FROM "..SDB.." ORDER BY ID DESC LIMIT 1" ))
if LastChallengeIDCreated2 == nil then
	LastChallengeIDCreated2 = 0
end

local LastChallengeIDCreated2 = (LastChallengeIDCreated2 + 1)
	--MsgN("[TDM-ChallengeP] ChallengeP Was Created Successfully, PlayerID: "..LastChallengeIDCreated2)
	MsgC(Color(255,255,255),"[",Color(255,0,0),"TDM-ChallengeAdd",Color(255,255,255),"] ChallengeP Was Created Successfully, PlayerID: ",Color(0,255,0),""..LastChallengeIDCreated2.."\n")
	sql.Query( "INSERT INTO "..SDB.." (`ID`, `PID`, `PName`, `C_Rank`, `C_Crouch`) VALUES ('"..tonumber(LastChallengeIDCreated2).."', '"..tostring(ply:UniqueID()).."', '"..tostring(ply:Nick()).."', '0', '0') ")
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawnTDMC", PlayerInitialSpawnTDMC )



function PlayerDeathUpdateSTDM( vic, wep, kil )
if !kil:IsPlayer() || !vic:IsPlayer() then 
return end
if file.Read("TDM/DM/ADM.txt") == "1" then
else
if vic:Team() == kil:Team() then
kil:SetFrags( kil:Frags() - 1 )
vic:SetDeaths( vic:Deaths()- 1 )
return end
end
if kil == vic then
return end


/*
First Kill Start
*/


if tonumber(file.Read("TDM/FirstK/FirstK.txt","DATA")) == 0 then
	file.Write("TDM/FirstK/FirstK.txt", 1)
	umsg.Start( "TDM_Player_IS_FK", kil )
	umsg.End()
	
	umsg.Start( "TDM_Players_IS_FK")
		umsg.String(kil:Name())
		umsg.Short(kil:Team())
	umsg.End()
end


/*
First Kill End
*/


/*
Kill Streaks Start
*/


kil:SetPData("TDMLastKill", (kil:GetPData("TDMLastKill")))
vic:SetPData("TDMLastKill", 0)


if (kil:GetPData("TDMLastKill") + 1) == 2 || (kil:GetPData("TDMLastKill") + 1) == "2" then
	if (math.Round(CurTime()) - kil:GetPData("TDMLastKillTime")) <= 5 then
		umsg.Start( "TDM_Player_IS_DK", kil )
		umsg.End()
	else
		kil:SetPData("TDMLastKill", 1)
	end
end

if (kil:GetPData("TDMLastKill") + 1) == 3 || (kil:GetPData("TDMLastKill") + 1) == "3" then
	if (math.Round(CurTime()) - kil:GetPData("TDMLastKillTime")) <= 5 then
		umsg.Start( "TDM_Player_IS_TK", kil )
		umsg.End()
	else
		kil:SetPData("TDMLastKill", 1)
	end
end

if (kil:GetPData("TDMLastKill") + 1) == 5 || (kil:GetPData("TDMLastKill") + 1) == "5" then
	if (math.Round(CurTime()) - kil:GetPData("TDMLastKillTime")) <= 4 then
		umsg.Start( "TDM_Player_IS_KC", kil )
		umsg.End()
		kil:SetPData("TDMLastKill", 0)
	elseif (math.Round(CurTime()) - kil:GetPData("TDMLastKillTime")) <= 10 then
		umsg.Start( "TDM_Player_IS_KS", kil )
		umsg.End()
		kil:SetPData("TDMLastKill", 0)
	else
		kil:SetPData("TDMLastKill", 1)
	end
end	

kil:SetPData("TDMLastKillTime", math.Round(CurTime()))



/*
Kill Streaks End
*/

 
if kil:Crouching() == true then
local GetKillersCrouchKils = sql.Query( "SELECT * FROM "..SDB.." WHERE PID = "..kil:UniqueID().."" )
	if !GetKillersCrouchKils then 
		MsgN("[TDM-ChallengeP] Player Does Not Exist!")
	return end

	for k, v in pairs( GetKillersCrouchKils ) do
	local GetCKil = tonumber(v['C_Crouch']) + 1
		sql.Query( "UPDATE "..SDB.." SET C_Crouch = "..GetCKil.." WHERE PID = "..kil:UniqueID().."" )
	--	MsgN(GetCKil)
		if GetCKil == 25 then
			umsg.Start( "TDMRoundedBoxHud2Ck5", kil )
				umsg.String("materials/tdmhud/chal/crouch.png" )
				umsg.String("Crouch Killer!")
				umsg.Short(1)
			umsg.End()
			
			local OldP = kil:GetPData("CurrentTDMPoints2")
			kil:SetPData("CurrentTDMPoints2",OldP+250)
			
			umsg.Start( "TDMRoundedBoxHud2CkSS", kil )
			umsg.End()
			
			timer.Simple(13, function()
			if !kil:IsValid() then return end
			umsg.Start( "TDMRoundedBoxHud2Ck5", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
				umsg.Short(0)
			umsg.End()
			end)
		elseif GetCKil/25 == 0.52 then
		end
	end
end


if kil:IsValid() then
local GetKillersRankKils = sql.Query( "SELECT * FROM "..SDB.." WHERE PID = "..kil:UniqueID().."" )
	if !GetKillersRankKils then 
		MsgN("[TDM-ChallengeP] Player Does Not Exist!")
	return end
	
	for k, v in pairs( GetKillersRankKils ) do
	local GetCRank = tonumber(v['C_Rank']) + 1
	sql.Query( "UPDATE "..SDB.." SET C_Rank = "..GetCRank.." WHERE PID = "..kil:UniqueID().."" )
	
	MsgN(GetCRank)
	
	if GetCRank == 10 || GetCRank == "10" then -- 1
	-- PRIVATE
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/1.png")
			umsg.String("Private")
			umsg.Short(1)
		umsg.End()
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Private")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 35 || GetCRank == "35" then -- 2
	-- PRIVATE FIRST CLASS
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/2.png")
			umsg.String("Private First Class")
			umsg.Short(1)
		umsg.End()
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Private First Class")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 80 || GetCRank == "80" then -- 3
	-- CORPORAL
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/3.png")
			umsg.String("Corporal")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Corporal")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 150 || GetCRank == "150" then -- 4
	-- SERGEANT
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/4.png")
			umsg.String("Sergeant")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Sergeant")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 250 || GetCRank == "250" then -- 5
	-- STAFF SERGEANT
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/5.png")
			umsg.String("Staff Sergeant")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Staff Sergeant")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 325 || GetCRank == "325" then -- 6
	-- GUNNERY SERGEANT
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/6.png")
			umsg.String("Gunnery Sergeant")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Gunnery Sergeant")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 475 || GetCRank == "475" then -- 7
	-- MASTER SERGEANT
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/7.png")
			umsg.String("Master Sergeant")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Master Sergeant")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 650 || GetCRank == "650" then -- 8
	-- SERGEANT MAJOR
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/8.png")
			umsg.String("Sergeant Major")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Sergeant Major")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 850 || GetCRank == "850" then -- 9
	-- SECOND LIEUTENANT
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/9.png")
			umsg.String("Second Lieutenant")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Second Lieutenant")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 1025 || GetCRank == "1025" then -- 10
	-- FIRST LIEUTENANT
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/10.png")
			umsg.String("First Lieutenant")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","First Lieutenant")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 1275 || GetCRank == "1275" then -- 11
	-- CAPTAIN
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/11.png")
			umsg.String("Captain")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Captain")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 1550 || GetCRank == "1550" then -- 12
	-- MAJOR
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/12.png")
			umsg.String("Major")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Major")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 1850 || GetCRank == "1850" then -- 13
	-- LIEUTENANT COLONEL
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/13.png")
			umsg.String("Lieutenant Colonel")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Lieutenant Colonel")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 2175 || GetCRank == "2175" then -- 14
	-- COLONEL
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/14.png")
			umsg.String("Colonel")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Colonel")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 2500 || GetCRank == "2500" then -- 15
	-- BRIGADIER GENERAL
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/15.png")
			umsg.String("Brigadier General")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Brigadier General")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 2850 || GetCRank == "2850" then -- 16
	-- MAJOR GENERAL
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/16.png")
			umsg.String("Major General")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Major General")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 3225 || GetCRank == "3225" then -- 17
	-- LIEUTENANT GENERAL
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/17.png")
			umsg.String("Lieutenant General")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","Lieutenant General")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	elseif GetCRank == 3500 || GetCRank == "3500" then -- 18
	-- COMMANDER
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("materials/tdmhud/rank/18.png")
			umsg.String("General")
			umsg.Short(1)
		umsg.End()
		
		
		kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+500)
		kil:SetPData("TDMRankNameUp","General")
		
		umsg.Start( "TDMRoundedBoxHud2CkS", kil )
		umsg.End()
		
		timer.Simple(13, function()
		if !kil:IsValid() then return end
		umsg.Start( "TDMRoundedBoxHud2Ck", kil )
			umsg.String("nil")
			umsg.String("materials/tdmhud/rank/test.png")
			umsg.Short(0)
		umsg.End()
		end)
	end
	
	end
end


/*
Update Points Start
*/


team.AddScore( kil:Team(), 1 ) 
if kil:GetPData("CurrentTDMPoints2") == nil then
	kil:SetPData("CurrentTDMPoints2",0)
	umsg.Start("apple_tdm_sendnewpoints", kil)
		umsg.Short(0)
	umsg.End()
else
kil:SetPData("CurrentTDMPoints2",(kil:GetPData("CurrentTDMPoints2"))+10)
	umsg.Start("apple_tdm_sendnewpoints", kil)
		umsg.Short((kil:GetPData("CurrentTDMPoints2"))+10)
	umsg.End()
end

if tonumber(team.GetScore(kil:Team())) == tonumber(file.Read("TDM/GoalP/GoalP.txt")) then
	NewMapName = table.Random( CustomMapsList3 )
	for k,v in pairs(player.GetAll()) do
	if NewMapName == nil || NewMapName == "" || NewMapName == " " then
		umsg.Start("apple_tdm_who_won", v)
			umsg.Short(kil:Team())
			umsg.Short(team.GetScore(kil:Team()))
			umsg.String(game.GetMap( ))
		umsg.End()
	else
		umsg.Start("apple_tdm_who_won", v)
			umsg.Short(kil:Team())
			umsg.Short(team.GetScore(kil:Team()))
			umsg.String(NewMapName)
		umsg.End()
	end
	end

	if NewMapName == nil then
		timer.Simple(31, function()
			game.ConsoleCommand("changelevel "..game.GetMap( ).."\n")
		end)
	else
		timer.Simple(31, function()
			game.ConsoleCommand("changelevel "..NewMapName.."\n")
		end)
	end
end

PointShopData = {}
for k,v in pairs(player.GetAll()) do
	PointShopData[k] = {
		Name = tostring(v:Nick()),
		Points = tostring(v:GetPData("CurrentTDMPoints2")),
		Rank = tostring(v:GetPData("TDMRankNameUp"))
	}
	
-- Update Labels Points
	if v:GetPData("TDMRankNameUp") == nil then
		v:SetPData("TDMRankNameUp","New Recruit")
		umsg.Start("apple_tdm_sendnewpointsjj")
			umsg.String(k)
			umsg.String("New Recruit")
		umsg.End()
	else
	umsg.Start("apple_tdm_sendnewpointsjj")
		umsg.String(k)
		umsg.String(v:GetPData("TDMRankNameUp"))
	umsg.End()
	end
-- Update Labels Name
	if v:GetPData("CurrentTDMPoints2") == nil then
		v:SetPData("CurrentTDMPoints2",0)
		umsg.Start("apple_tdm_sendnewpointsj")
			umsg.String(k)
			umsg.String("0")
		umsg.End()
	else
		umsg.Start("apple_tdm_sendnewpointsj")
			umsg.String(k)
			umsg.String(v:GetPData("CurrentTDMPoints2")+10)
		umsg.End()
	end
end


PrintTable(PointShopData)
net.Start( "apple_tdm_pointshopdata3" )
	net.WriteTable(PointShopData)
net.Broadcast()


umsg.Start("apple_tdm_psppoints", kil)
	umsg.String(kil:GetPData("CurrentTDMPoints2")+10)
	umsg.String(kil:GetPData("TDMRankNameUp"))
umsg.End()
end
hook.Add( "PlayerDeath", "PlayerDeathUpdateSTDM", PlayerDeathUpdateSTDM )


/*
Update Points End
*/